package com.panopset;

import static com.panopset.Util.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;

import javax.swing.JLabel;

import com.panopset.gui.TLabel;
import com.panopset.gui.TPanel;


public class SysInfoPanel {

    private TPanel gui;

    public TPanel getGui() {
        if (gui == null) {
            gui = new TPanel.Builder().layout(new GridBagLayout()).construct();
            Insets leftInsets = new Insets(0,20,0,0);

            GridBagConstraints gbc = new GridBagConstraints();
            int gridy = 0;

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel("Name: "), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            String name = "Un-able to determine, see log.";
            try {
                name = InetAddress.getLocalHost().getCanonicalHostName();
            } catch (UnknownHostException e) {
                log(e);
            }
            gui.add(new TLabel(name), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel("Processors: "), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(EMPTY_STRING + Runtime.getRuntime().availableProcessors()), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel("Total memory:"), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(EMPTY_STRING + Runtime.getRuntime().totalMemory()/1024 + " kilobytes."), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel("Max memory:"), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(EMPTY_STRING + Runtime.getRuntime().maxMemory()/1024 + " kilobytes."), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel("Free memory:"), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(EMPTY_STRING + Runtime.getRuntime().freeMemory()/1024 + " kilobytes."), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel("Operating system:"), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(System.getProperty("os.name") + SINGLE_SPACE +
                    System.getProperty("sun.os.patch.level")), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel(x("CPU architecture:")), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(System.getProperty("os.arch") + COMMA +
                    EMPTY_STRING + EMPTY_STRING + System.getProperty("sun.arch.data.model")), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel(x("CPU compatibility:")), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(System.getProperty("sun.cpu.isalist")), gbc);

            gbc.gridx = 0;
            gbc.gridy = gridy;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.insets = leftInsets;
            gui.add(new JLabel(x("Language:")), gbc);

            gbc.gridx = 1;
            gbc.gridy = gridy++;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            gui.add(new TLabel(Locale.getDefault().getLanguage()), gbc);
        }
        return gui;
    }

}
